<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>websocket测试</title>
</head>
<script type="text/javascript">
    var socket;
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:8888/ws");
        socket.onopen = function (ev) {
            var ta = document.getElementById("responseText");
            ta.value = "链接已经建立";
        }
        socket.onmessage = function (ev) {
            var ta = document.getElementById("responseText");
            ta.value = ta.value + "\n" + ev.data;
        }
        socket.onclose = function (ev) {
            var ta = document.getElementById("responseText");
            ta.value = ta.value + "\n链接已经关闭";
        }
    } else {
        alert("链接已经关闭");
    }

    function send(msg) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState === WebSocket.OPEN) {
            socket.send(msg);
        }
    }


</script>
<body>

<form>
    <textarea id="message" style="width: 150px;height: 150px;"></textarea>
    <input type="button" value="发送" onclick="send(this.form.message.value)"/>
    <textarea id="responseText" style="width: 150px;height: 150px;"></textarea>
</form>
</body>
</html>